<template>
  <div class="address">
    <div class="tou">
      <div class="header">
        <div class="back" @click="$router.go(-1)">
          <img src="@/assets/back.png" alt="" />
        </div>
        <div class="title">城市选择</div>
      </div>

      <div class="search">
        <input
          v-model="keyword"
          class="search-input"
          type="text"
          placeholder="输入城市名或拼音"
        />
      </div>
    </div>
    <div class="search-content" ref="search" v-show="keyword">
      <ul>
        <li
          class="search-item border-bottom"
          v-for="item of list"
          :key="item.id"
          v-show="list.length"
          @click="goHome(item.name)"
        >
          {{ item.name }}
        </li>
        <li class="search-item border-bottom" v-show="!list.length">
          没有找到匹配数据
        </li>
      </ul>
    </div>
    <div v-show="!keyword" class="seart">
      <p>当前城市</p>
      <div class="danq">
        <div class="btt" v-if="val">{{ val }}</div>
        <div class="btt" v-else>暂无</div>
      </div>
      <!-- <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">北京</div>
          </div>
          <div class="button-wrapper">
            <div class="button">西安</div>
          </div>
          <div class="button-wrapper">
            <div class="button">北京</div>
          </div>
          <div class="button-wrapper">
            <div class="button">北京</div>
          </div>
          <div class="button-wrapper">
            <div class="button">北京</div>
          </div>
          <div class="button-wrapper">
            <div class="button">北京</div>
          </div>
        </div>
      </div> -->
      <van-index-bar>
        <div v-for="(item, key, index) in json.data.cities" :key="index">
          <van-index-anchor :index="key" />
          <van-cell
            :title="itemArr.name"
            v-for="itemArr in item"
            :key="itemArr.id"
            @click="goHome(itemArr.name)"
          />
        </div>
      </van-index-bar>
    </div>
  </div>
</template>

<script>
import json from "../../../../static/mock/city.json";
export default {
  data() {
    return {
      json,
      keyword: "",
      list: [],
    };
  },
  created() {
    console.log(this.$route.query.val);
  },
  computed: {
    val() {
      return this.$route.query.val;
    },
  },
  methods: {
    goHome(val) {
      this.$router.push({
        path: "/",
        query: {
          val,
        },
      });
    },
  },
  watch: {
    keyword() {
      if (!this.keyword) {
        this.list = [];
        return;
      }
      if (this.timer) {
        clearTimeout(this.timer);
      }

      this.timer = setTimeout(() => {
        const result = [];
        for (let i in this.json.data.cities) {
          this.json.data.cities[i].forEach((value) => {
            if (
              value.spell.indexOf(this.keyword) > -1 ||
              value.name.indexOf(this.keyword) > -1
            ) {
              result.push(value);
            }
          });
        }
        this.list = result;
      }, 100);
    },
  },
};
</script>

<style scoped lang="scss">
.tou{
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 999;
}
.address {
  width: 100%;
}
.header {
  padding: 0 20px;
  height: 44px;
  background: #00bcd4;
  color: #fff;
  display: flex;
  align-items: center;
  .back {
    height: 20px;
    img {
      width: 20px;
      height: 20px;
    }
  }
  .title {
    flex: 1;
    text-align: center;
  }
}
.search {
  height: 50px;
  padding: 5px;
  background: #00bcd4;
  .search-input {
    box-sizing: border-box;
    width: 100%;
    height: 40px;
    padding: 0 5px;
    line-height: 40px;
    text-align: center;
    border-radius: 10px;
    color: #666;
  }
}
.danq {
  height: 60px;
  background-color: #fff;
  padding-left: 20px;
  display: flex;
  align-items: center;
  .btt {
    width: 100px;
    height: 35px;
    line-height: 35px;
    text-align: center;
    border: 1px solid #000;
  }
}
.search-content {
  margin-top: 104px;
  height: auto;
  li {
    list-style: none;
    height: 40px;
    line-height: 40px;
    background-color: #fff;
  }
}
.seart{
  margin-top: 104px;
}
</style>